package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.common.PageInfo;
import com.smart.model.dtomapper.StreetDtoMapper;
import com.smart.model.domain.StreetDO;
import com.smart.mapper.StreetMapper;
import com.smart.model.request.StreetRequest;
import com.smart.service.StreetService;
import com.smart.model.convert.StreetConvert;
import com.smart.model.vo.StreetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class StreetServiceImpl extends ServiceImpl<StreetMapper, StreetDO> implements StreetService {
    @Autowired
    private StreetMapper streetMapper;

    @Override
    public StreetVO getByKey(long id) {
        StreetDO streetDO = getById(id);
        StreetVO streetVO = StreetDtoMapper.INSTANCE.do2VO(streetDO);
        return streetVO;
    }

    @Override
    public PageInfo<StreetVO> pageQuery(StreetRequest request) {
        long count = streetMapper.pageCount(request);
        List<StreetDO> list = streetMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<StreetVO> streetVOList = StreetConvert.buildStreetVoList(list);
        return new PageInfo(request.getPageNum(), request.getPageSize(), streetVOList, count);
    }

    @Override
    public List<StreetVO> listQuery(StreetRequest request) {
        List<StreetDO> list = list();
        List<StreetVO> streetVOS = StreetConvert.buildStreetVoList(list);
        return streetVOS;
    }

    @Override
    public Boolean add(StreetRequest request) {
        StreetDO streetDO = StreetDtoMapper.INSTANCE.request2DO(request);
        save(streetDO);
        return true;
    }

    @Override
    public Boolean updateByKey(StreetRequest request) {
        StreetDO streetDO = StreetDtoMapper.INSTANCE.request2DO(request);
        save(streetDO);
        return true;
    }

    @Override
    public Boolean deleteByKey(String code) {
        streetMapper.deleteDOByCode(code);
        return true;
    }

    @Override
    public List<StreetVO> queryStreetByAreaCode(String areaCode) {
        List<StreetDO> streetDOList = this.lambdaQuery().eq(StreetDO::getAreaCode, areaCode).list();
        return StreetConvert.buildStreetVoList(streetDOList);
    }
}
